package com.Saenko.GPS;


import java.util.Date;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;


public class Utilities {
	/**
	 * Converts given meters to feet.
	 * 
	 * @param m
	 * @return
	 */
	public static int MetersToFeet(int m)
	{
		return (int) Math.round(m * 3.2808399);
	}

	/**
	 * Converts given feet to meters
	 * 
	 * @param f
	 * @return
	 */
	public static int FeetToMeters(int f)
	{
		return (int) Math.round(f / 3.2808399);
	}

	/**
	 * Converts given meters to feet and rounds up.
	 * 
	 * @param m
	 * @return
	 */
	public static int MetersToFeet(double m)
	{
		return MetersToFeet((int) m);
	}

	/**
	 * Converts given feet to meters and rounds up.
	 * 
	 * @param f
	 * @return
	 */
	public static int FeetToMeters(double f)
	{
		return FeetToMeters((int) f);
	}
	
	public static boolean Flag()
	{
		return false;
	}
	
	/**
	 * Converts given bearing degrees into a rough cardinal direction that's
	 * more understandable to humans.
	 * 
	 * @param bearingDegrees
	 * @return
	 */
	public static String GetBearingDescription(float bearingDegrees, Context context)
	{

		String direction;
		String cardinal;

		if (bearingDegrees > 348.75 || bearingDegrees <= 11.25)
		{
			cardinal = context.getString(R.string.direction_north);
		}
		else if (bearingDegrees > 11.25 && bearingDegrees <= 33.75)
		{
			cardinal = context.getString(R.string.direction_northnortheast);
		}
		else if (bearingDegrees > 33.75 && bearingDegrees <= 56.25)
		{
			cardinal = context.getString(R.string.direction_northeast);
		}
		else if (bearingDegrees > 56.25 && bearingDegrees <= 78.75)
		{
			cardinal = context.getString(R.string.direction_eastnortheast);
		}
		else if (bearingDegrees > 78.75 && bearingDegrees <= 101.25)
		{
			cardinal = context.getString(R.string.direction_east);
		}
		else if (bearingDegrees > 101.25 && bearingDegrees <= 123.75)
		{
			cardinal = context.getString(R.string.direction_eastsoutheast);
		}
		else if (bearingDegrees > 123.75 && bearingDegrees <= 146.26)
		{
			cardinal = context.getString(R.string.direction_southeast);
		}
		else if (bearingDegrees > 146.25 && bearingDegrees <= 168.75)
		{
			cardinal = context.getString(R.string.direction_southsoutheast);
		}
		else if (bearingDegrees > 168.75 && bearingDegrees <= 191.25)
		{
			cardinal = context.getString(R.string.direction_south);
		}
		else if (bearingDegrees > 191.25 && bearingDegrees <= 213.75)
		{
			cardinal = context.getString(R.string.direction_southsouthwest);
		}
		else if (bearingDegrees > 213.75 && bearingDegrees <= 236.25)
		{
			cardinal = context.getString(R.string.direction_southwest);
		}
		else if (bearingDegrees > 236.25 && bearingDegrees <= 258.75)
		{
			cardinal = context.getString(R.string.direction_westsouthwest);
		}
		else if (bearingDegrees > 258.75 && bearingDegrees <= 281.25)
		{
			cardinal = context.getString(R.string.direction_west);
		}
		else if (bearingDegrees > 281.25 && bearingDegrees <= 303.75)
		{
			cardinal = context.getString(R.string.direction_westnorthwest);
		}
		else if (bearingDegrees > 303.75 && bearingDegrees <= 326.25)
		{
			cardinal = context.getString(R.string.direction_northwest);
		}
		else if (bearingDegrees > 326.25 && bearingDegrees <= 348.75)
		{
			cardinal = context.getString(R.string.direction_northnorthwest);
		}
		else
		{
			direction = context.getString(R.string.unknown_direction);
			return direction;
		}

		direction = context.getString(R.string.direction_roughly, cardinal);
		return direction;

	}
	/**
	 * Displays a message box to the user with an OK button.
	 * 
	 * @param title
	 * @param message
	 * @param className
	 *            The calling class, such as GpsMainActivity.this or
	 *            mainActivity.
	 */
	public static void MsgBox(String title, String message, Context className)
	{
		MsgBox(title, message, className, null);
	}
	/**
	 * Displays a message box to the user with an OK button.
	 * 
	 * @param title
	 * @param message
	 * @param className
	 *            The calling class, such as GpsMainActivity.this or
	 *            mainActivity.
	 * @param callback
	 *            An object which implements IHasACallBack so that the click
	 *            event can call the callback method.
	 */
	public static void MsgBox(String title, String message, Context className,
			final IMessageBoxCallback msgCallback)
	{
		AlertDialog alertDialog = new AlertDialog.Builder(className).create();
		alertDialog.setTitle(title);
		alertDialog.setMessage(message);
		alertDialog.setButton(className.getString(R.string.ok), new DialogInterface.OnClickListener()
		{
			public void onClick(DialogInterface dialog, int which)
			{
				if (msgCallback != null)
				{
					msgCallback.MessageBoxResult(which);
				}
			}
		});
		alertDialog.show();
	}
	
	/*
	 * LatLon 0 = Latitude, 1 = Longitude
	 */
	public static Coordinate GetCoordinate(String convert, int LatLon) {
		Coordinate coord = new Coordinate();
		if (convert.lastIndexOf('-') > 0)
		{
			if (LatLon == 0){coord.setNSEW('S');}
			else {coord.setNSEW('W');}
		}
		else
		{
			if (LatLon == 0){coord.setNSEW('N');}
			else {coord.setNSEW('E');}
		}
		String [] temp = null;
		temp = convert.split(":");
		String hours = temp[0];
		if (LatLon == 1)
		{
			if (Integer.parseInt(hours) < 100) {hours = "0"+hours;}
		}
		String minutes = temp[1];
		minutes = minutes.replace(',', '.');
		  
		coord.setHour(Integer.parseInt(hours));
		coord.setMinute(Double.parseDouble(minutes));
		  
		return coord;
	}

	/** Calculates the checksum for a sentence */
	public static String getChecksum(String sentence) {
		// Loop through all chars to get a checksum
		char character;
		int checksum = 0;
		int length = sentence.length();
		for (int i = 0; i < length; i++) {
			character = sentence.charAt(i);
			switch (character) {
				case '$':
					// Ignore the dollar sign
					break;
				case '*':
					// Stop processing before the asterisk
					break;
				default:
					// Is this the first value for the checksum?
					if (checksum == 0) {
						// Yes. Set the checksum to the value
						checksum = (byte) character;
					} else {
						// No. XOR the checksum with this character's value
						checksum = checksum ^ ((byte) character);
					}
			}
		}
		// Return the checksum formatted as a two-character hexadecimal
		return Integer.toHexString(checksum);
	}

	public static String GetFullTime(Date fulldate) {
		String hours;
		if (fulldate.getHours()<10)
		{
			hours = "0"+fulldate.getHours();
		}
		else
		{
			hours = Integer.toString(fulldate.getHours());
		}
		
		String minutes;
		if (fulldate.getMinutes()<10)
		{
			minutes = "0"+fulldate.getMinutes();
		}
		else
		{
			minutes = Integer.toString(fulldate.getMinutes());
		}
		
		String seconds;
		if (fulldate.getSeconds()<10)
		{
			seconds = "0"+fulldate.getSeconds();
		}
		else
		{
			seconds = Integer.toString(fulldate.getSeconds());
		}
		return hours+minutes+seconds;
		
	}
	
	public static String GetFullDate(Date fulldate)
	{
		String day;
		if (fulldate.getDay()<10)
		{
			day = "0"+fulldate.getDay();
		}
		else
		{
			day = Integer.toString(fulldate.getDay());
		}	
		String month;
		if (fulldate.getMonth()<10)
		{
			month = "0"+Integer.toString(fulldate.getMonth());
		}
		else
		{
			month = Integer.toString(fulldate.getMonth());
		}	
		String year=Integer.toString(fulldate.getYear());
		year = Character.toString(year.charAt(1)) +Character.toString(year.charAt(2));
			
			
		return day+month+year;
	}
	
	public static double MetersPerSecToKilometersPerHour(double speed)
	{
		return speed * 3.6;
	} 
}
